

#include "HttpServer.hpp"

#define WWWROOT "./wwwroot"

std::string RequestStr(const HttpRequest &requ)
{
    std::stringstream ss;
    ss << requ._method << " " << requ._path << " " << requ._version << "\r\n";
    ss << "\r\n";
    for (auto &head : requ._headers)
    {
        ss << head.first << ": " << head.second << "\r\n";
    }
    ss << "\r\n";
    for (auto &param : requ._params)
    {
        ss << param.first << ": " << param.second << "\r\n";
    }
    ss << "\r\n";
    ss << requ._body;
    return ss.str();
}

void Hello(const HttpRequest &requ, HttpResponse *resp)
{
    resp->SetContent(RequestStr(requ), "text/plain");
}

void Login(const HttpRequest &requ, HttpResponse *resp)
{
    LOG(HDEBUG, "Login\n");
    resp->SetContent(RequestStr(requ), "text/plain");
}

void PutFile(const HttpRequest &requ, HttpResponse *resp)
{
    std::string pathname = WWWROOT + requ._path;
    Util::WriteFile(pathname, requ._body);
}

void DeleteFile(const HttpRequest &requ, HttpResponse *resp)
{
    resp->SetContent(RequestStr(requ), "text/plain");
}

int main()
{
    HttpServer server(8888);
    server.SetThreadCount(3);
    server.SetBaseDir(WWWROOT);
    server.GET("/hello", Hello);
    server.POST("/login", Login);
    server.PUT("/1234.txt", PutFile);
    server.DELETE("/1234.txt", DeleteFile);
    server.Listen();
    return 0;
}
